package com.hundsun.cprs.yyt.service.system.service.impl;

import com.hundsun.cprs.yyt.common.base.BaseService;
import com.hundsun.cprs.yyt.service.system.dao.TradeDayDao;
import com.hundsun.cprs.yyt.service.system.domain.TradeDay;
import com.hundsun.cprs.yyt.service.system.domain.TradeDayCurrent;
import com.hundsun.cprs.yyt.service.system.domain.query.TradeDayQuery;
import com.hundsun.cprs.yyt.service.system.service.TradeDayCurrentService;
import com.hundsun.cprs.yyt.service.system.service.TradeDayService;
import com.hundsun.cprs.yyt.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class TradeDayServiceImpl extends BaseService implements TradeDayService {

    @Autowired
    private TradeDayDao tradeDayDao;
    @Autowired
    private TradeDayCurrentService tradeDayCurrentService;

    @Override
    @Transactional
    public void addTradeDay(List<Date> list, int nowYear) {
        TradeDayCurrent current = tradeDayCurrentService.getCurrentTradeDay();
        TradeDay param = new TradeDay();
        param.setYear(nowYear);
        param.setDay(current.getNextTradeDay());
        tradeDayDao.removeTradeDayByYear(param);

        Calendar cal = Calendar.getInstance();
        for (Date day : list) {
            cal.setTime(day);
            TradeDay tradeDay = new TradeDay();
            tradeDay.setDay(day);
            tradeDay.setMonth(cal.get(Calendar.MONTH) + 1);
            tradeDay.setYear(nowYear);
            tradeDayDao.addTradeDay(tradeDay);
        }
    }

    @Override
    public List<TradeDay> getTradeDay(TradeDay tradeDay) {
        if (tradeDay == null) {
            return new ArrayList<TradeDay>();
        }
        return tradeDayDao.getTradeDay(tradeDay);
    }

    @Override
    @Transactional
    public void addWorkDay(Date startDate) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(startDate);
        int year = cal.get(Calendar.YEAR);
        TradeDayCurrent current = tradeDayCurrentService.getCurrentTradeDay();
        Date nextTradeDay = current.getNextTradeDay();
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(nextTradeDay);
        int currentYear = cal2.get(Calendar.YEAR);

        TradeDay param = new TradeDay();
        param.setYear(year);
        if (year == currentYear) {
            param.setDay(nextTradeDay);
            cal.setTime(nextTradeDay);
            cal.add(Calendar.DAY_OF_MONTH, +1);
        }
        tradeDayDao.removeTradeDayByYear(param);

        while (cal.get(Calendar.YEAR) < year + 1) {
            if (cal.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY
                    && cal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
                TradeDay tradeDay = new TradeDay();
                tradeDay.setDay(cal.getTime());
                tradeDay.setMonth(cal.get(Calendar.MONTH) + 1);
                tradeDay.setYear(cal.get(Calendar.YEAR));
                tradeDayDao.addTradeDay(tradeDay);
            }
            cal.add(Calendar.DAY_OF_MONTH, +1);
        }
    }

    @Override
    public void queryByPage(TradeDayQuery query) {
        List<TradeDay> tradeDayList = tradeDayDao.queryByPage(query);
        query.compatible(tradeDayList);
    }

    @Override
    public Boolean isClearingDate(Date date) {
        Boolean flag = true;
        try{
            TradeDay tradeDay = new TradeDay();
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            tradeDay.setDay(DateUtil.convertStringToDate(df.format(date)));
            List<TradeDay> tradeDayList = tradeDayDao.getTradeDay(tradeDay);
            if (CollectionUtils.isEmpty(tradeDayList)){
                flag = false;
            }
        }catch (ParseException e){
            logger.error(e);
            flag = false;
        }
        return flag;
    }

    @Override
    public TradeDay getNextClearingDay(Date date) {
        return tradeDayDao.getNextTradeDay(date);
    }

    @Override
    public void updateTradeStatus(TradeDay tradeDay) {
        tradeDayDao.updateTradeStatus(tradeDay);
    }
}
